import Vue from 'vue'

Vue.filter('langFormat', function(labelStruct,lang) {
  var struct = {"Default":""};
  try{
    struct = JSON.parse(labelStruct);
  }catch (e){
    struct = {"Default":""};
  }
  var label = "";
  if(!lang)
    lang = "Default";

  if(struct[lang])
    label = struct[lang];
  return label;
});

Vue.filter('NumFormat', function(value) {
  if(!value) return '0.00';

  var intPart = Number(value).toFixed(0); //获取整数部分
  var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); //将整数部分逢三一断

  var floatPart = ".00"; //预定义小数部分
  var value2Array = value.split(".");

  //=2表示数据有小数位
  if(value2Array.length == 2) {
    floatPart = value2Array[1].toString(); //拿到小数部分

    if(floatPart.length == 1) { //补0,实际上用不着
      return intPartFormat + "." + floatPart + '0';
    } else {
      return intPartFormat + "." + floatPart;
    }

  } else {
    return intPartFormat + floatPart;
  }

})

Vue.filter('dateFormat', function(dateTime,fmt) {
  var date =null;
  if(dateTime instanceof Date)
    date = dateTime;
  else
    date = new Date(dateTime);

  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  var o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (var k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      var str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : commonJs.padLeftZero(str));
    }
  }
  return fmt;
})

Vue.filter('bigMoneyFormat', function(mnum) {
  var mnum = parseFloat(mnum);
  var strOutput="",strTemp="",strInTemp='';
  var unitArray = new Array("圆万亿","仟佰拾","零壹贰叁肆伍陆柒捌玖");
  var mnumArray = mnum.toString().split('.');
  var integralnum = mnumArray[0];
  var integrallen = integralnum.length;
  var decimalnum = (mnum.toString().indexOf('.')>=0) ? mnumArray[1].substr(0, 2) : '0';
  var decimallen = decimalnum.length;
  var ints = parseInt(integrallen/4);
  var inty = integrallen%4;
  if (ints>3 || (ints==3 && inty>0)) return "超出范围";
  if (inty>0)
  {
    ints++;
    integralnum = "0000".substr(inty)+integralnum;
    integrallen = integralnum.length;
  }
  var i = 0;
  while (i<integrallen)
  {
    var strOutTemp = "";
    strTemp = integralnum.substr(i, 4);
    i += 4;
    for (var j=0; j<4; j++)
    {
      strInTemp = parseInt(strTemp.substr(j, 1));
      strOutTemp += unitArray[2].substr(strInTemp, 1);
      if (strInTemp>0 && j<3) strOutTemp += unitArray[1].substr(j, 1);
    }
    strOutTemp = strOutTemp.replace(/零+$/, "");
    ints--;
    if (strOutTemp!="") strOutTemp += unitArray[0].substr(ints, 1);
    if (strTemp.substr(3,1)=='0') strOutTemp += "零";
    strOutput += strOutTemp;
  }
  strOutput = strOutput.replace(/零+/g, "零").replace(/^零/, "").replace(/零$/, "");
  if (strOutput=="圆") strOutput = "";
  if (decimallen==2)
  {
    strOutput += (decimalnum.charAt(0)!='0') ? unitArray[2].substr(parseInt(decimalnum.charAt(0)), 1)+"角" : "零";
    if (strOutput=="零") strOutput = "";
    strOutput += unitArray[2].substr(parseInt(decimalnum.charAt(1)), 1)+"分";
  }
  else
  {
    if (decimalnum!='0') strOutput += unitArray[2].substr(parseInt(decimalnum), 1)+"角";
    if (strOutput!="") strOutput += "整";
  }
  if (strOutput=="") strOutput = "金额为零";
  return strOutput;
})



Vue.filter('money', function(val) {
  val = val.toString().replace(/\$|\,/g,'');
  if(isNaN(val)) {
    val = "0";
  }
  let sign = (val == (val = Math.abs(val)));
  val = Math.floor(val*100+0.50000000001);
  let cents = val%100;
  val = Math.floor(val/100).toString();
  if(cents<10) {
    cents = "0" + cents
  }
  for (var i = 0; i < Math.floor((val.length-(1+i))/3); i++) {
    val = val.substring(0,val.length-(4*i+3))+',' + val.substring(val.length-(4*i+3));
  }

  return (((sign)?'':'-') + val + '.' + cents);
})

